<?php

namespace App\Http\Controllers\Admin;

use Illuminate\Http\Request;

use App\Http\Requests;
use App\Http\Controllers\Controller;
use App\Libraries\ValidateCode;
use DB;
use Session;
// 导入 Intervention Image Manager Class
use Intervention\Image\ImageManagerStatic as Image;

class ClassifyController extends Controller
{
    //分类视图
    public function index()
    {
    	$cdata = DB::table('crowd') -> get();
    	
    	foreach ($cdata as $key => $val) {
    		$d = DB::table('cro_spo_rel')
    			-> leftJoin('sport','cro_spo_rel.spo_id','=','sport.sport_id')
    		 	-> where('cro_id',$val -> crowd_id) -> get();
    		$cdata[$key] ->sub = $d;
    	}


    	$pdata = DB::table('sport') -> get();

    	foreach($pdata as $key => $val)
    	{
    		$d = DB::table('cro_spo_rel')
    			->leftjoin('crowd','crowd.crowd_id','=','cro_spo_rel.cro_id')
    			->where('spo_id',$val -> sport_id) ->get();
    			$pdata[$key] -> sub = $d;
    	}
    	
    	$bdata = DB::table('brand') -> get();
    	
    	return view('admin/product/classify/index',['cdata' => $cdata,'pdata'=>$pdata,'bdata'=>$bdata]);
    }


    //品牌修改
    public function brandedit($id)
    {
    	//echo $id;
    	$title = '品牌编辑';
    	$data = DB::table('brand') -> where('brand_id','=',$id) -> first();
    	// dd($data);
    	//return view('admin/product/classify/create',['data'=>$data,'title'=>$title]);
    	return view('admin/product/classify/brandedit',['data'=>$data,'title'=>$title]);
   	}


   	//品牌更新
   	public function brandupdate(Request $request)
   	{
   		$data = $request -> except('_token','old_logo');
        // dd($data);
   		//图片处理
        //处理pic
        if($request -> hasFile('brand_logo'))
        {
            if($request -> file('brand_logo') -> isValid())
            {
                //获取图片的后缀名
                $suffix = $request -> file('brand_logo') -> getClientOriginalExtension();
                // echo $suffix;
                
                //随即一个文件名
                $fileName = time().mt_rand(100000,999999).'.'.$suffix;


                // $path = date('Ymd');
                $path = './uploads/product/brand';
                // $fname = $path . '/' . $fileName;
                
                $request -> file('brand_logo') -> move($path,$fileName);
                $size = getimagesize($path.'/'.$fileName);
                
                //获取想要缩放的比例
                $w = 100;
                $h = 100;
                if($w/$size[0] > $h/$size[1])
                {
                    $bili = $h/$size[1];
                }else{
                    $bili = $w/$size[0];
                }
                
                //最终的比例
                $nw = floor($size[0]*$bili);
                $nh = floor($size[1]*$bili);
                

                $data['brand_logo'] = $fileName;

                $fname = $path . '/' . $fileName;
                $img = Image::make($fname)->resize($nw,$nh)->save($fname);
                // echo $data['pic'];
                if(file_exists('./uploads/product/brand/'.$request->old_logo)){
                    unlink('./uploads/product/brand/'.$request->old_logo);
                }
            }
        }

        //更新数据
        $res = DB::table('brand') -> where('brand_id','=',$data['brand_id']) -> update($data);
        // dd($res);
        if($res)
        {
	        return redirect('admin/classify') -> with(['success'=>'更新成功']);
	    }else{
	        return back() -> with(['error'=>'更新失败']);
	    }
    	

   	}


    //商品属性classifyattr
    public function classifyattr(Request $request)
    {
    	//查询颜色
    	$cdata = DB::table('color') -> select('color_id','color_name_en','color_name_zh','color_hex_code') -> get();
    	// dd($data);
    	
    	//查询size
    	$sdata = DB::table('size') -> select('size_id','size_size','size_pro_code') -> get();
    	return view('admin/product/classify/classifyattr',['cdata'=>$cdata,'sdata'=>$sdata]);
    }


}
